import java.io.*;

/**
 * Copyright: Copyright (c) 2018 Ztesoft
 *
 * @ClassName: PACKAGE_NAME.SerializableTest
 * @Description: 该类的功能描述
 * @version: v1.0.0
 * @author: wangtongbang
 * @date: 2018/6/27 10:08
 * <p>
 * Modification History:
 * Date         Author          Version            Description
 * ------------------------------------------------------------
 * 2018/6/27      wangtongbang          v1.0.0               修改原因
 */
public class SerializableTest {
    public static void main(String[] args) throws Exception{

        writeSerializableFileToDisk();
        readSerializableFileFromDisk();

    }


    private static void readSerializableFileFromDisk() throws Exception{
        File file = new File("1.txt");


        FileInputStream fi = null;
        ObjectInputStream ois = null;
        try {
            fi = new FileInputStream(file);
            ois = new ObjectInputStream(fi);
            Person obj = (Person)ois.readObject();
            System.out.println(obj.toString());

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fi.close();
                ois.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void writeSerializableFileToDisk() throws Exception{
        File file = new File("1.txt");

        Person xiaoming = new Person("小明",1,"北京朝阳村");

        FileOutputStream fo = null;
        ObjectOutputStream oop = null;
        try {
            fo = new FileOutputStream(file);
            oop = new ObjectOutputStream(fo);
            oop.writeObject(xiaoming);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e){
            e.printStackTrace();
        }finally {
            try {
                fo.close();
                oop.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
